/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.mts.common.utils;

import java.math.BigDecimal;

/**
 * Created by Peter on 2016/7/15.
 */
public class MathUtils {

    public static double[] convertBigDecimalToDoubleArray(BigDecimal[] data) {
        return MathUtils.convertBigDecimalToDoubleArray(data, 0);
    }

    public static double[] convertBigDecimalToDoubleArray(BigDecimal[] data, int digitNum) {
        double[] result = new double[data.length];

        for (int i = 0; i < data.length; i++) {
            data[i].setScale(digitNum, BigDecimal.ROUND_HALF_UP).doubleValue();
        }
        return result;
    }

    public static BigDecimal[] convertDoubleToBigDecimalArray(double[] data) {
        BigDecimal[] result = new BigDecimal[data.length];
        for (int i = 0; i < data.length; i++) {
            result[i] = new BigDecimal(data[i]);
        }
        return result;
    }

    public static BigDecimal[] convertDoubleToBigDecimalArray(double[] data, int digitNum) {
        BigDecimal[] result = new BigDecimal[data.length];
        for (int i = 0; i < data.length; i++) {
            result[i] = new BigDecimal(data[i]).setScale(digitNum, BigDecimal.ROUND_HALF_UP);
        }
        return result;
    }

    public static double formatDouble(double dataValue, int digitNum) {
        if (digitNum > 0) {
            return new BigDecimal(dataValue).setScale(digitNum, BigDecimal.ROUND_HALF_UP).doubleValue();
        } else {
            return dataValue;
        }
    }

    public static void main(String[] args) {
        double[] d = new double[]{1D, 2D, 2.4556756, 4.443434};
        BigDecimal[] dd = MathUtils.convertDoubleToBigDecimalArray(d);
        for (int i = 0; i < dd.length; i++) {
            System.out.println(dd[i]);
        }

    }
}
